package com.atguigu.controller;

import com.atguigu.constant.MessageConstant;
import com.atguigu.constant.RedisMessageConstant;
import com.atguigu.utils.SMSUtils;
import com.atguigu.utils.ValidateCodeUtils;
import com.atguigu.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

/**
 * @program: meinian_parent
 * @author: Mr.Zhuo
 * @create: 2022-02-17 21:14
 **/


@RestController
@RequestMapping("/validateCode")
public class ValidateCodeController {

    @Autowired
    private JedisPool jedisPool;

    // 预约套餐的时候发送短信
    @GetMapping("/send4Order")
    public Result send4Order(@RequestParam("telephone") String telephone) {
        // 使用工具类获取六位数短信验证码
        Integer code = ValidateCodeUtils.generateValidateCode(6);
        // 发送短信
        try {
            SMSUtils.sendShortMessage(telephone, code.toString());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }

        // 存储到redis中
        jedisPool.getResource().setex(telephone + RedisMessageConstant.SENDTYPE_ORDER, 5 * 60, code.toString());
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }
    // 快速注册的时候发送短信
    @GetMapping("/send4OrderLogin")
    public Result send4OrderLogin(@RequestParam("telephone") String telephone) {
        // 使用工具类获取六位数短信验证码
        Integer code = ValidateCodeUtils.generateValidateCode(6);
        // 发送短信
        try {
            SMSUtils.sendShortMessage(telephone, code.toString());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }

        // 存储到redis中
        jedisPool.getResource().setex(telephone + RedisMessageConstant.SENDTYPE_LOGIN, 5 * 60, code.toString());
        return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
    }
}
